package com.jblank.core.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.jblank.common.utils.JsonHelper;
import com.jblank.common.utils.WebUtils;
import com.jblank.dao.Dao;
import com.jblank.dto.DTO;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * Action基类
 * 
 * @author kim
 * @since 2013-07-22
 * 
 */
public class BizAction extends ActionSupport implements SessionAware,
		ServletRequestAware, ServletResponseAware, ServletContextAware,
		ModelDriven<DTO> {

	private static final long serialVersionUID = 1L;

	protected Map<String, Object> session;

	protected HttpServletRequest request;

	protected HttpServletResponse response;

	protected ServletContext context;

	protected DTO dto;

	protected Dao dao;

	public Dao getDao() {
		return dao;
	}

	@Resource(name = "daoOracle")
	public void setDao(Dao dao) {
		this.dao = dao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts2.util.ServletContextAware#setServletContext(javax.servlet.ServletContext)
	 */
	public void setServletContext(ServletContext context) {
		this.context = context;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts2.interceptor.ServletResponseAware#setServletResponse(javax.servlet.http.HttpServletResponse)
	 */
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
	 */
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts2.interceptor.SessionAware#setSession(java.util.Map)
	 */
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@SuppressWarnings("unchecked")
	public DTO getModel() {
		dto = WebUtils.getParamAsDto(request);
		return null;
	}

	/**
	 * 获取上下文
	 * 
	 * @return ApplicationContext
	 */
	public ApplicationContext getApplicationContext() {
		return WebApplicationContextUtils.getWebApplicationContext(context);
	}

	/**
	 * 获取spring中的bean
	 * 
	 * @param beanName
	 * @return Object
	 */
	public Object getObject(String beanName) {
		return getApplicationContext().getBean(beanName);
	}

	/**
	 * 输出响应
	 * 
	 * @param str
	 * @throws IOException
	 */
	protected void write(Object obj, HttpServletResponse response)
			throws IOException {
		response.getWriter().print(obj);
		response.getWriter().flush();
		response.getWriter().close();
	}

	/**
	 * 直接将List转为分页�?��要的Json资料格式
	 * 
	 * @param list
	 *            编码的List对象
	 * @param totalCount
	 *            记录总数
	 * @param pDataFormat
	 *            时间日期格式?传null则表明List不包含日期时间属�?
	 */
	@SuppressWarnings("unchecked")
	protected String encodeList2PageJson(List list, Integer totalCount,
			String dataFormat) {
		return JsonHelper.encodeList2PageJson(list, totalCount, dataFormat);
	}

	/**
	 * 将数据系列化为表单数据填充Json格式
	 * 
	 * @param pObject
	 *            待系列化的对象
	 * @param pFormatString
	 *            日期时间格式?如果为null则认为没有日期时间型字段
	 * @return
	 */
	protected String encodeDto2FormLoadJson(DTO pDto, String pFormatString) {
		return JsonHelper.encodeDto2FormLoadJson(pDto, pFormatString);
	}
	
	protected JSONObject encodeList2PageJson(List list,Integer totalCount,String[] flds){
		JSONArray rows = new JSONArray();
		for(int i = 0; i < list.size(); i++){
			Map p = (Map)list.get(i);
			JSONArray row = new JSONArray();
			for(int j = 0; j < flds.length;j++){
				row.add(p.get(flds[j]));
			}
			rows.add(row);
		}
		JSONObject ret = new JSONObject();
		ret.put("count", totalCount);
		ret.put("rows", rows);
		return ret;
	}

	/**
	 * 将数据系列化为Json格式
	 * 
	 * @param pObject
	 *            待系列化的对象
	 * @param pFormatString
	 *            日期时间格式?如果为null则认为没有日期时间型字段
	 * @return
	 */
	protected String encodeObject2Json(Object pObject, String pFormatString) {
		return JsonHelper.encodeObject2Json(pObject, pFormatString);
	}

	/**
	 * 将数据系列化为Json格式
	 * 
	 * @param pObject
	 *            待系列化的对象
	 * @return
	 */
	protected String encodeObjectJson(Object pObject) {
		return JsonHelper.encodeObject2Json(pObject);
	}
}
